from PySide2 import QtCore
from PySide2 import QtGui
from PySide2.QtCore import *  # type: ignore
from PySide2.QtGui import *  # type: ignore
from PySide2.QtWidgets import *  # type: ignore
from PySide2.QtWidgets import (
    QApplication,
    QMainWindow,
    QPushButton,
    QLabel,
    QVBoxLayout,
    QWidget,
    QListWidgetItem,
)
from ubl_settings_manager import Ui_MainWindow
import sys
import typing
import PySide2
from config import *
from Dialog import Ui_Dialog
import c_code
import sys


class Me_ListWidgetItem(QListWidgetItem):
    def __init__(
        self, text: str, listview: typing.Optional[QListWidget] = ..., type: int = ...
    ) -> None:
        super().__init__(text, listview, type)
        self.setViewMode(QListView.IconMode)
        self.setSpacing(10)
        self.setIconSize(QSize(50, 50))


class GUI_front(Ui_MainWindow):
    def __init__(self) -> None:
        super().__init__()
        self.main_setting()
        self.link_doc_app = ""

    def main_setting(self):
        self.config = read_json(f"{path}/locale/config.json")
        self.list_icons = self.config["icon"]
        self.call = self.config
        self.main_wind = QMainWindow()
        self.item = QListWidgetItem("")
        self.item_2 = QListWidgetItem("")
        self.item_3 = QListWidgetItem("")
        self.item_4 = QListWidgetItem("")
        self.setting()
        self.setupUi(self.main_wind)
        self.set_data(self.call)
        self.add_icon()
        self.pushButton_close.clicked.connect(exit)
        self.pushButton.clicked.connect(self.dialog)
        self.listWidget.itemClicked.connect(self.listWidget.clicked)
        self.listWidget_2.itemClicked.connect(self.listWidget_2.clicked)
        self.listWidget_3.itemClicked.connect(self.listWidget_3.clicked)
        self.listWidget_4.itemClicked.connect(self.listWidget_4.clicked)
        self.listWidget.itemDoubleClicked.connect(self.listWidget.doubleClicked)
        self.listWidget_2.itemDoubleClicked.connect(self.listWidget_2.doubleClicked)
        self.listWidget_3.itemDoubleClicked.connect(self.listWidget_3.doubleClicked)
        self.listWidget_4.itemDoubleClicked.connect(self.listWidget_4.doubleClicked)
        obj_signal.text.connect(self.__get_str)

    def enter_icon(self, dict_icon):
        if dict_icon:
            list_key_icon = list(dict_icon.keys())
            list_key_icon.sort(reverse=True)
            return dict_icon[list_key_icon[0]]
        else:
            return ""

    def add_icon(self):
        for dict_icon in self.list_icons:
            icon = self.enter_icon(dict_icon["icon"])
            text = dict_icon["text"]
            for categories in dict_icon["Categories"]:
                if categories == "Личные":
                    self.item.setIcon(QtGui.QIcon(icon))
                    self.item.setText(text)
                    self.listWidget.addItem(self.item)
                if categories == "Оборудование":
                    self.item_2.setText(text)
                    self.item_2.setIcon(QtGui.QIcon(icon))
                    self.listWidget_2.addItem(self.item_2)
                if categories == "Система":
                    self.item_3.setText(text)
                    self.item_3.setIcon(QtGui.QIcon(icon))
                    self.listWidget_3.addItem(self.item_3)
                if categories == "Прочее":
                    self.item_4.setText(text)
                    self.item_4.setIcon(QtGui.QIcon(icon))
                    self.listWidget_4.addItem(self.item_4)

    def dialog(self):
        print(self.config["doc"])
        if self.config["doc"]:
            self.button_doc()
        else:
            self.main_wind_1 = QMainWindow()
            form = Ui_Dialog()
            form.setupUi(self.main_wind_1)
            form.buttonBox.button(QDialogButtonBox.Ok).clicked.connect(self.button_doc)
            form.buttonBox.button(QDialogButtonBox.Cancel).clicked.connect(exit)
            form.checkBox.toggled.connect(self.cheek_button_doc)
            self.main_wind_1.show()

    def cheek_button_doc(self):
        self.config["doc"] = True
        write_json(f"{path}/locale/config.json", self.config)
        self.button_doc()

    def __get_str(self, text):
        self.link_doc_app = text
        # print(self.link_doc_app, 222222)

    def button_doc(self):

        # print(self.link_doc_app, 222222)
        # path_url = self.config["path_utl"]
        link = f"xdg-open {self.link_doc_app}"
        print(link)
        link = link.encode("utf-8")
        #
        c_code.obj_c_tools.execute(link)
        self.link_doc_app = ""


if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = GUI_front()
    form.main_wind.show()
    sys.exit(app.exec_())
